package com.xxj.puzzlesheep.core;


import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

/**
 * 
 * Copyright 2008 - 2009
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @project loonframework
 * @author chenpeng
 * @email ceponline@yahoo.com.cn
 * @version 0.1.0
 */
public class LGraphics {

	private Bitmap bitmap;

	private Canvas canvas;

	private Paint paint;

	private Rect clip;

	public LGraphics(Bitmap bitmap) {
		this.bitmap = bitmap;
		this.canvas = new Canvas(bitmap);
		this.canvas.clipRect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		this.canvas.save(Canvas.CLIP_SAVE_FLAG);
		this.paint = new Paint();
		this.clip = canvas.getClipBounds();
	}

	public void setAntiAlias(boolean flag) {
		paint.setAntiAlias(flag);
	}

	public void setAlphaValue(int alpha) {
		paint.setAlpha(alpha);
	}

	public void setAlpha(float alpha) {
		setAlphaValue((int) (255 * alpha));
	}

	public void setColor(int rgb) {
		paint.setColor(rgb);
	}

	public void drawImage(LImage img, int x, int y) {
		canvas.drawBitmap(img.getBitmap(), x, y, paint);
	}

	public void drawLine(int x1, int y1, int x2, int y2) {
		if (x1 > x2) {
			x1++;
		} else {
			x2++;
		}
		if (y1 > y2) {
			y1++;
		} else {
			y2++;
		}
		canvas.drawLine(x1, y1, x2, y2, paint);
	}

	public void drawRect(int x, int y, int width, int height) {
		paint.setStyle(Paint.Style.STROKE);
		canvas.drawRect(x, y, x + width, y + height, paint);
	}

	public void drawString(String str, int x, int y) {
		canvas.drawText(str, x, y, paint);
	}

	public void drawClear() {
		paint.setColor(Color.BLACK);
		canvas.drawColor(Color.BLACK);
	}

	public void fillRect(int x, int y, int width, int height) {
		paint.setStyle(Paint.Style.FILL);
		canvas.drawRect(x, y, x + width, y + height, paint);
	}

	public int getClipHeight() {
		return clip.bottom - clip.top;
	}

	public int getClipWidth() {
		return clip.right - clip.left;
	}

	public int getClipX() {
		return clip.left;
	}

	public int getClipY() {
		return clip.top;
	}

	public int getColor() {
		return paint.getColor();
	}

	public void dispose() {
		paint = null;
		canvas = null;
	}

	public void clearRect(int x, int y, int width, int height) {
		canvas.clipRect(x, y, x + width, y + height);
		canvas.drawARGB(0xff, 0xff, 0xff, 0xff);
	}

	public void copyArea(int x, int y, int width, int height, int dx, int dy) {
		if (x < 0) {
			width += x;
			x = 0;
		}
		if (y < 0) {
			height += y;
			y = 0;
		}
		if (x + width > bitmap.getWidth()) {
			width = bitmap.getWidth() - x;
		}
		if (y + height > bitmap.getHeight()) {
			height = bitmap.getHeight() - y;
		}
		Bitmap copy = Bitmap.createBitmap(bitmap, x, y, width, height);
		canvas.drawBitmap(copy, x + dx, y + dy, null);
	}

	public void clipRect(int x, int y, int width, int height) {
		canvas.clipRect(x, y, x + width, y + height);
		clip = canvas.getClipBounds();
	}

	public void setClip(int x, int y, int width, int height) {
		if (x == clip.left && x + width == clip.right && y == clip.top
				&& y + height == clip.bottom) {
			return;
		}
		if (x < clip.left || x + width > clip.right || y < clip.top
				|| y + height > clip.bottom) {
			canvas.restore();
			canvas.save(Canvas.CLIP_SAVE_FLAG);
		}
		clip.left = x;
		clip.top = y;
		clip.right = x + width;
		clip.bottom = y + height;
		canvas.clipRect(clip);
	}

	public void drawOval(int x, int y, int width, int height) {
		paint.setStyle(Paint.Style.STROKE);
		canvas.drawOval(new RectF(x, y, x + width, y + height), paint);
	}

	public void drawPolygon(int[] xpoints, int[] ypoints, int npoints) {
		canvas.drawLine(xpoints[npoints - 1], ypoints[npoints - 1], xpoints[0],
				ypoints[0], paint);
		for (int i = 0; i < npoints - 1; i++) {
			canvas.drawLine(xpoints[i], ypoints[i], xpoints[i + 1],
					ypoints[i + 1], paint);
		}
	}

	public Canvas getCanvas() {
		return canvas;
	}

	public Paint getPaint() {
		return paint;
	}

	public Rect getClip() {
		return clip;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}

}
